//Secondary Custom Code 2 - Start
//START - Drop-list which dynamically fills-in the NAMES of Primary, CoApplicant 1, CoApplicant 2, CoSigner 1, etc…
function DropListNameSelection()
{
  var A_NAME = this.getField("A_NAME");
  var CSTM_NAME = this.getField("CSTM_NAME");
  var prevName = "";
  var checkNames = ["Select Member Name"];//Makes sure all values in drop list are unique

  //If the drop list has a value from being processed again, prevName will store the value
  if (CSTM_NAME.value != ""){
    prevName = CSTM_NAME.value;
  }
  
  if (A_NAME.value != ""){
    CSTM_NAME.insertItemAt(A_NAME.value, A_NAME.value, -1);//Inserts name at the end of the drop list
    checkNames.push(A_NAME.value);
  }
  
  //The number 5 must be updated depending on how many JNi_NAME mapped on the document.
  for (var i = 1; i <= 5; i++) {
    if (this.getField("JN" + i + "_NAME").value != "" && checkNames.indexOf(this.getField("JN" + i + "_NAME").value) < 0){ //Fills the name in the drop list only if it has not already been added to the drop list.
      CSTM_NAME.insertItemAt(this.getField("JN" + i + "_NAME").value, this.getField("JN" + i + "_NAME").value, -1);//Inserts name at the end of the drop list
      checkNames.push(this.getField("JN" + i + "_NAME").value);
    }
  }

  //The number 5 must be updated depending on how many ASi_NAME mapped on the document.
  for (var i = 1; i <= 5; i++)  {
    if (this.getField("AS" + i + "_NAME").value != ""  && checkNames.indexOf(this.getField("AS" + i + "_NAME").value) < 0){ //Fills the name in the drop list only if it has not already been added to the drop list.
      CSTM_NAME.insertItemAt(this.getField("AS" + i + "_NAME").value, this.getField("AS" + i + "_NAME").value, -1);//Inserts name at the end of the drop list
      checkNames.push(this.getField("AS" + i + "_NAME").value);
    }
  }
    //Retains the drop list value when reprocessed 
    if (prevName != "" && prevName != "Select Member Name" && prevName != "[None]"){
      CSTM_NAME.value = prevName;
    }else{
      CSTM_NAME.currentValueIndices = 0;//Sets the current value of the drop list to the first option
    }
}
//END - Drop-list which dynamically fills-in the NAMES of Primary, CoApplicant 1, CoApplicant 2, CoSigner 1, etc…

// *Don’t call below function on load*
//START - Depending on whose name is selected in Drop-list FIELD dynamically fills-in the names

function CSTM_NAME_PDFBlur()
{
  FirstBorrowerSelected();
}

function FirstBorrowerSelected()
{
  var CSTM_NAME = this.getField("CSTM_NAME");
  var CSTM_SIG_NAME = this.getField("CSTM_SIG_NAME");
  
  for (var i = 1; i <= 3; i++){
    this.getField("TEAWEB_SIG_A_" + i + "_TXT").value = 0;

    this.getField("TEAWEB_SIG_JN1_" + i + "_TXT").value = 0;
    this.getField("TEAWEB_SIG_JN2_" + i + "_TXT").value = 0;
    this.getField("TEAWEB_SIG_JN3_" + i + "_TXT").value = 0;
    this.getField("TEAWEB_SIG_JN4_" + i + "_TXT").value = 0;
    this.getField("TEAWEB_SIG_JN5_" + i + "_TXT").value = 0;

    this.getField("TEAWEB_SIG_AS1_" + i + "_TXT").value = 0;
    this.getField("TEAWEB_SIG_AS2_" + i + "_TXT").value = 0;
    this.getField("TEAWEB_SIG_AS3_" + i + "_TXT").value = 0;
    this.getField("TEAWEB_SIG_AS4_" + i + "_TXT").value = 0;
    this.getField("TEAWEB_SIG_AS5_" + i + "_TXT").value = 0;
  }
  CSTM_SIG_NAME.value = "";
  
  //New account checked
  if (this.getField("CSTM_NEW_FLAG").value == "1"){
    if(CSTM_NAME.value == this.getField("A_NAME").value) 
    {
      this.getField("TEAWEB_SIG_A_1_TXT").value = 1;
    	CSTM_SIG_NAME.value = this.getField("A_NAME").value;  	  
    }
    else {
    			for (var i = 1; i <= 5; i++) {
    			  if (CSTM_NAME.value == this.getField("JN" + i + "_NAME").value) 
            {
    				  this.getField("TEAWEB_SIG_JN" + i + "_1_TXT").value = 1; 
    				  CSTM_SIG_NAME.value = this.getField("JN" + i + "_NAME").value;
              break;//If the value is set, exit the for loop
    			  }
    			}

    			for (var i = 1; i <= 5; i++) {
    			  if (CSTM_NAME.value == this.getField("AS" + i + "_NAME").value) 
            {
    				  this.getField("TEAWEB_SIG_AS" + i + "_1_TXT").value = 1;
    				  CSTM_SIG_NAME.value = this.getField("AS" + i + "_NAME").value;
              break;//If the value is set, exit the for loop
    			  }
    			}
	  }
  }

  //Modify terms of agreement checked
  if (this.getField("CSTM_MODIFY_FLAG").value == "1"){
    if(CSTM_NAME.value == this.getField("A_NAME").value) 
    {
      this.getField("TEAWEB_SIG_A_2_TXT").value = 1;
    	CSTM_SIG_NAME.value = this.getField("A_NAME").value;  	  
    }
    else {
    			for (var i = 1; i <= 5; i++) {
    			  if (CSTM_NAME.value == this.getField("JN" + i + "_NAME").value) 
            {
    				  this.getField("TEAWEB_SIG_JN" + i + "_2_TXT").value = 1; 
    				  CSTM_SIG_NAME.value = this.getField("JN" + i + "_NAME").value;
              break;//If the value is set, exit the for loop
    			  }
    			}

    			for (var i = 1; i <= 5; i++) {
    			  if (CSTM_NAME.value == this.getField("AS" + i + "_NAME").value) 
            {
    				  this.getField("TEAWEB_SIG_AS" + i + "_2_TXT").value = 1;
    				  CSTM_SIG_NAME.value = this.getField("AS" + i + "_NAME").value;
              break;//If the value is set, exit the for loop
    			  }
    			}
	  }
  }

  //Termination of agreement checked
  if (this.getField("CSTM_TERMINATION_FLAG").value == "1"){
    if(CSTM_NAME.value == this.getField("A_NAME").value) 
    {
      this.getField("TEAWEB_SIG_A_3_TXT").value = 1;
    	CSTM_SIG_NAME.value = this.getField("A_NAME").value;  	  
    }
    else {
    			for (var i = 1; i <= 5; i++) {
    			  if (CSTM_NAME.value == this.getField("JN" + i + "_NAME").value) 
            {
    				  this.getField("TEAWEB_SIG_JN" + i + "_3_TXT").value = 1; 
    				  CSTM_SIG_NAME.value = this.getField("JN" + i + "_NAME").value;
              break;//If the value is set, exit the for loop
    			  }
    			}

    			for (var i = 1; i <= 5; i++) {
    			  if (CSTM_NAME.value == this.getField("AS" + i + "_NAME").value) 
            {
    				  this.getField("TEAWEB_SIG_AS" + i + "_3_TXT").value = 1;
    				  CSTM_SIG_NAME.value = this.getField("AS" + i + "_NAME").value;
              break;//If the value is set, exit the for loop
    			  }
    			}
	  }
  }

}  
	

//END - Depending on whose name is selected in Drop-list FIELD dynamically fills-in the names

function CSTM_NAME_PDFMouseUp()
{
  this.getField("VALIDATE_SIGNERS_FIELD").value = ""; //Clears the name selection validation if the user chooses a different name.
}

function VALIDATE_SIGNERS_BTN_PDFMouseUp()
{
  FirstBorrowerSelected();
  this.getField("VALIDATE_SIGNERS_FIELD").value = "VALIDATED";
}

function CSTM_NEW_FLAG_PDFBlur()
{
  FirstBorrowerSelected();
}

function CSTM_MODIFY_FLAG_PDFBlur()
{
  FirstBorrowerSelected();
}

function CSTM_TERMINATION_FLAG_PDFBlur()
{
  FirstBorrowerSelected();
}
//Secondary Custom Code 2 - End